package unipi.database;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Rights
{
	public enum RightCols
	{
		TaskRights {public String toString(){return "taskRights";}},
		UserRights 	{public String toString(){return "userRights";}},
		DptRights 	{public String toString(){return "dptRights";}},	
	}
	
	public static Map<String, Boolean> getRights(String uid)
	{
		int ncols = 3;
		String query = "select \r\n" + 
						"coalesce(sum(taskRights), 0),\r\n" + 
						"coalesce(sum(userRights), 0),\r\n" + 
						"coalesce(sum(dptRights), 0)\r\n" + 
						"from UserRightsPerDpt\r\n" + 
						"where userID = " + uid;
		
		List<List<String>> result = SQLMethods.Select(query, ncols);
		
		ncols = 1;
		query = "select childUserID from usertaskRights\r\n" + 
				"where parentUserID = " + uid;
		
		List<List<String>> peruserresult = SQLMethods.Select(query, ncols);
		
		if(result == null) return null;
		if(result.size() == 0) return null;
		
		if(peruserresult == null) return null;
		
		Map<String, Boolean> map = new HashMap<String, Boolean>();
		
		map.put("taskRights", Integer.parseInt(result.get(0).get(0)) != 0 || peruserresult.size() != 0);
		map.put("userRights", Integer.parseInt(result.get(0).get(1)) != 0);
		map.put("dptRights", Integer.parseInt(result.get(0).get(2)) != 0);
		
		return map;
	}
	
	public static boolean hasTaskRights(String uid)
	{
		Map<String, Boolean> map = getRights(uid);
		
		if(map == null) return false;
		
		return map.get("taskRights");		
	}
	
	public static ErrorCodes editRights(String rid, boolean task, boolean user, boolean dpt)
	{
		if(!task && !user && !dpt)
		{
			return SQLMethods.Delete("UserRightsperDpt", "id = " + rid)!= -1? ErrorCodes.OK: ErrorCodes.databaseError;
		}
		else
		{		
			String[] columns = {RightCols.TaskRights.toString(), RightCols.UserRights.toString(), RightCols.DptRights.toString()};
			String[] newvalues = {Boolean.toString(task), Boolean.toString(user), Boolean.toString(dpt)};
			
			return SQLMethods.Update("UserRightsperDpt", columns, newvalues, "id = " + rid)
									!= -1? ErrorCodes.OK: ErrorCodes.databaseError;	
		}
	}
	
	public static ErrorCodes giveDptRights(String uid, String dptID, RightCols rights)
	{		
		int ncols = 1;
		String query = "select id from userRightsperDpt where userID = " + uid + " and dptID = " + dptID;		
		List<List<String>> rightsList = SQLMethods.Select(query, ncols);
		
		if(rightsList == null) return ErrorCodes.databaseError;
		
		if(rightsList.size() == 0)
		{
			String columns = "userID, dptID," + rights;			
			String values = uid + "," + dptID + ", true";
			
			return SQLMethods.Insert("userRightsperDpt", columns, values) != -1? ErrorCodes.OK: ErrorCodes.databaseError;				
		}
		else
		{
			String rightID = rightsList.get(0).get(0);

			return SQLMethods.Update("userRightsperDpt", rights.toString(), "true", "id = " + rightID) != -1?
																				ErrorCodes.OK: ErrorCodes.databaseError;			
		}
	
	}
	
	public static ErrorCodes giveUserRights(String parentUser, String childUser)
	{
		String columns = "parentUserID, childUserID";			
		String values = parentUser + " , " +  childUser;
		
		
		String query = "select parentUserID from userTaskRights " +
				"where parentUserID = " + parentUser + " and childUserID = " + childUser;
		List<List<String>> res = SQLMethods.Select(query, 1);
		
		if(res == null) return null;
		if(res.size() != 0) return ErrorCodes.duplicateUsernames;
		
		return SQLMethods.Insert("userTaskRights", columns, values) != -1? ErrorCodes.OK: ErrorCodes.databaseError;		
	}
	
	public static ErrorCodes removeUserRights(String parentUser, String childUser)
	{
		return SQLMethods.Delete("UserTaskRights", "parentUserID = " + parentUser + " and childUserID = " + childUser) != -1?
				ErrorCodes.OK: ErrorCodes.databaseError;	
		
	}
	
	public static List<List<String>> getEditRightsPerDpt(String uid)
	{
		int ncols = 3;
		String query = "select\r\n" + 
				"concat('<a class=\"genericLink\" href=\"Profile?id=', t1.uid, '\">', Users.surname, ' ', Users.name, '</a>'), \r\n" + 
				"t1.name,\r\n" + 
				"concat('<form name=\"fm_', t1.rid, '\" action=\"EditRights\" method=\"post\">" +
				"Εργασιών: <input type=\"checkbox\" name=\"taskRights\" value=\"tr\"', if(t1.taskRights = true, 'checked=\"checked\"', '') ,'/> | " + 
				"Χρηστών: <input type=\"checkbox\" name=\"userRights\" value=\"ur\"', if(t1.userRights = true, 'checked=\"checked\"', '') ,'/> | " + 
				"Τμημάτων: <input type=\"checkbox\" name=\"dptRights\" value=\"dr\"', if(t1.dptRights = true, 'checked=\"checked\"', '') ,'/> | " + 
				"<input type=\"hidden\" name=\"rid\" value=\"', t1.rid, '\" />" +
				"&nbsp;&nbsp;<a class=\"genericLink\" href=\"#\" onclick=\"document.fm_', t1.rid,'.submit()\">Ενημερωση</a></form>')\r\n" + 
				"from\r\n" + 
				"Users inner join\r\n" + 
				"(\r\n" + 
				"    select u.id as uid, ur.taskRights, ur.userRights, ur.dptRights, ur.dptID, d.name, ur.id as rid from \r\n" + 
				"    userRightsPerDpt ur\r\n" + 
				"    inner join\r\n" + 
				"    (\r\n" + 
				"        select id\r\n" + 
				"        from ActiveUsers where id != 94 and \r\n" + 
				"        dptID in\r\n" + 
				"        (\r\n" + 
				"            select r.dptID\r\n" + 
				"            from userrightsperdpt r\r\n" + 
				"            where userID = " + uid + " and userRights = true \r\n" + 
				"        )\r\n" + 
				"    ) u\r\n" + 
				"    on ur.userID = u.id\r\n" + 
				"    inner join Departments d\r\n" + 
				"    on d.id = ur.dptID\r\n" + 
				") t1\r\n" + 
				"on Users.id = t1.uid\r\n" + 
				"where Users.id != " + uid;
		
		return SQLMethods.Select(query, ncols);		
	}
	
	public static List<List<String>> getEditRightsPerUser(String uid)
	{
		int ncols = 3;
		String query = "select \r\n" + 
						"concat('<a class=\"genericLink\" href=\"Profile?id=',t.parentUserID, '\">', t.puname, '</a>'), \r\n" + 
						"concat('<a class=\"genericLink\" href=\"Profile?id=',t.childUserID, '\">', u.surname, ' ', u.name, '</a>'),\r\n" + 
						"concat('<form action=\"EditRights\" method=\"post\" name=\"fmu_', t.ParentUserID, '_', t.childUserID, '\">\r\n" + 
						"<input name=\"uridP\" type=\"hidden\" value=\"', t.ParentUserID,'\" />\r\n" + 
						"<input name=\"uridC\" type=\"hidden\" value=\"', t.childUserID,'\" />\r\n" + 
						"<input name=\"urid\" type=\"hidden\" value=\"urid\" />\r\n" + 
						"<a class=\"genericLink\" href=\"#\" onclick=\"document.fmu_', t.ParentUserID, '_', t.childUserID,'.submit()\">" +
						"Αφαίρεση</a></form>')\r\n" + 
						"from\r\n" + 
						"Users u inner join\r\n" + 
						"(\r\n" + 
						"    select usr.puname, rpu.parentUserID, rpu.childUserID from \r\n" + 
						"    userTaskRights rpu inner join\r\n" + 
						"    (\r\n" + 
						"        select concat(u.surname, ' ', u.name) as puname, u.id from\r\n" + 
						"        Users u inner join\r\n" + 
						"        (\r\n" + 
						"            select rpd.dptID from userRightsPerDpt rpd\r\n" + 
						"            where rpd.userID = " + uid + " and userRights = true\r\n" + 
						"        ) dpts\r\n" + 
						"        on dpts.dptID = u.dptID\r\n" + 
						"    ) usr\r\n" + 
						"    on rpu.parentUserID = usr.id\r\n" + 
						") t\r\n" + 
						"on t.childUserID = u.id\r\n" +
						"where t.ParentUserID != " + uid ;		
		
		return SQLMethods.Select(query, ncols);	
	}
	
	public static List<List<String>> getUsers(String uid, RightCols rights)
	{
		int ncols = 2;		
	
		String query = "select id, concat(surname,' ',name) from ActiveUsers where " +
						"id != " + uid + " and " +
						"dptID in\r\n" + 
						"(\r\n" + 
						"    select r.dptID from userrightsperdpt r\r\n" + 
						"	 where userID = " + uid + " and " + rights + " = true )"; 
		
		if(rights == RightCols.TaskRights)
		{
			query += " or id in \r\n" + 
					"(\r\n" + 
					"    select childUserID from usertaskrights\r\n" + 
					"    where parentUserID = " + uid  + ")";			
		}
		
		
		return SQLMethods.Select(query, ncols);
	}
	
	public static List<List<String>> getDpts(String uid, RightCols rights)
	{
		int ncols = 2;
		
		String query = "select d.id, d.name from userRightsperDpt r\r\n" + 
						"inner join Departments d\r\n" + 
						"on d.id = r.dptID\r\n" + 
						"where " + rights + " = true and r.userID = " + uid;
		
		
		return SQLMethods.Select(query, ncols);
	}

}
